package com.hoku.account.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

import javax.servlet.http.HttpServletResponse;

/**
 * 资源服务配置
 * @author ZHUFEIFEI
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Value("${management.endpoints.web.base-path}")
    private String managementContextPath;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        //健康监控允许匿名访问
        http.authorizeRequests().antMatchers(managementContextPath + "/**").permitAll();
        http.authorizeRequests().anyRequest().authenticated();
        http.exceptionHandling().accessDeniedHandler((request, response, authException)
                -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED));
        http.httpBasic();
    }

}
